package heap;

import global.AttrType;
import global.EID;
import global.PID;
import global.PageId;
import global.RID;

import java.io.IOException;

public class TemporaryBasicPattern extends Tuple{

	private static short numFlds = 1;
	private static short [] strSizes = {203};
	private static AttrType [] types = {new AttrType (AttrType.attrString)};
	private int counter = 0;	
	private boolean isFirst = true;
	private StringBuffer strData = null;
	public int _tupleLength = 0;
	
	private TemporaryBasicPattern(int size, short [] strSizes) throws InvalidTypeException, InvalidTupleSizeException, IOException {
		// TODO Auto-generated constructor stub
		super (size);
		counter = 0;
		isFirst = true;
		strData = new StringBuffer();
		this.setHdr(numFlds, types, strSizes);
	}
	
	public static TemporaryBasicPattern getInstance (int numCols) throws InvalidTypeException, InvalidTupleSizeException, IOException {
		int dataLength = 50;
		int tupleLength = dataLength * numCols;
		//For delimitters
		tupleLength += numCols-1;
		short [] strSizes = {(short) tupleLength};
		TemporaryBasicPattern ttp = new TemporaryBasicPattern(tupleLength+8, strSizes);
		ttp._tupleLength = tupleLength;
		return ttp;
	}
	
	public static void addString (TemporaryBasicPattern tbp, String key) throws FieldNumberOutOfBoundException, IOException {
		if (!tbp.isFirst) {
			tbp.strData.append('^');
		} else {
			tbp.isFirst = false;
		}
		tbp.strData.append(key);
		tbp.counter++;
		while (tbp.strData.length() < tbp.counter*50) {
			tbp.strData.append(' ');
		}
	}
	
	public void setString () throws FieldNumberOutOfBoundException, IOException {
		//System.out.println("Setting string:" + strData.toString().trim());
		setStrFld(1, strData.toString().trim());
	}
	
	
	public TemporaryBasicPattern (Tuple tup) {
		data = tup.getTupleByteArray();
	    tuple_length = tup.getLength();
	    tuple_offset = 0;
	    fldCnt = tup.noOfFlds(); 
	    fldOffset = tup.copyFldOffset();
	}
	
	public TemporaryBasicPattern (byte[] b, int i, int j) {
		// TODO Auto-generated constructor stub
		super(b, i, j);
	}

	public void printTuple () throws FieldNumberOutOfBoundException, IOException {
		System.out.println(this.getStrFld(1));
	}
	
	public void addTriple (String data) throws FieldNumberOutOfBoundException, IOException {		
		this.setStrFld (1, data);		
	}
	
	public void addTriple (String data1, String data2, String data3, String data4) throws FieldNumberOutOfBoundException, IOException {
		StringBuffer data = new StringBuffer(data1);
		while (data.length() < 50)
			data.append (' ');//(Character.MIN_VALUE);
		data.append ('^');
		data.append(data2);
		while (data.length() < 100)
			data.append (' ');//(Character.MIN_VALUE);
		data.append ('^');
		data.append(data3);
		while (data.length() < 150)
			data.append (' ');//(Character.MIN_VALUE);
		data.append ('^');
		data.append(data4);
		//System.out.println("Key length is: " + data.toString().trim ().length() + "Data: " + data.toString().trim ());
		this.setStrFld (1, data.toString().trim ());		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

